//
//  WDCityListController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/19.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDCityListController.h"
#import "WDTreeTableView.h"
#import "WDNode.h"
#import "WDCityList.h"
#import "WDProvinceList.h"

@interface WDCityListController () <WDTreeTableCellDelegate>

/** 省份数据 */
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation WDCityListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadNavItemWithAction:@selector(backBtn) btnImage:@"icon_back" btnSelImage:@"icon_back" title:@"可抵押城市"];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self loadTreeTableView];
}

- (void)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadTreeTableView
{
    WDTreeTableView *tableView = [[WDTreeTableView alloc] initWithFrame:self.view.bounds withData:self.data];
    
    tableView.treeTableCellDelegate = self;
    [self.view addSubview:tableView];
}

#pragma mark - lazy load
- (NSMutableArray *)data
{
    if (_data == nil) {
        _data = [NSMutableArray array];
        
        WDProvinceList *listP = [[WDProvinceList alloc] init];
        WDCityList *listC = [[WDCityList alloc] init];
        
        static int node = 0;
        node += self.provinces.count;
        
        for (int i = 0; i < self.provinces.count; i++) {
            listP = self.provinces[i];
            NSString *pName = listP.name;
            
            WDNode *province = [[WDNode alloc] initWithParentId:-1 nodeId:i name:pName depth:0 expand:YES];
            
            [_data addObject:province];
            
            NSArray *citys = [WDCityList mj_objectArrayWithKeyValuesArray:listP.city_list];
            
            for (int j = 0; j < citys.count; j++) {
                listC = citys[j];
                NSString *cName = listC.name;
                
                WDNode *city = [[WDNode alloc] initWithParentId:i nodeId:node name:cName depth:1 expand:NO];
                
                [_data addObject:city];
                node++;
            }
        }
    }
    return _data;
}

#pragma mark - TreeTableCellDelegate
- (void)cellClick:(WDNode *)node
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
